//
//  MOVersionInfoViewController.m
//  MobileOffice
//
//  Created by 晓 on 2019/6/7.
//  Copyright © 2019 com.idolmedia. All rights reserved.
//

#import "MOVersionInfoViewController.h"

@interface MOVersionInfoViewController ()

@property(nonatomic,strong)UILabel *textLabel;

@property(nonatomic,strong)UILabel *desclLabel;

@end

@implementation MOVersionInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initView];
    
    [self initData];
}

- (void)initView{
    self.title = @"版本信息";
    self.view.backgroundColor = KBackGrayColor;
    
    UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(0, 10, SCREEN_WIDTH, kHConversions(252))];
    backView.backgroundColor = KWhiteColor;
    
    UIImageView *iconImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, kHConversions(60), kHConversions(79), kHConversions(79))];
    iconImageView.centerX = SCREEN_WIDTH/2;
    iconImageView.image = IMAGE(@"appVersion");
    
    self.textLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, CGRectGetMaxY(iconImageView.frame) + 18, SCREEN_WIDTH, kHConversions(62))];
    //    self.textLabel.text = @"国家税务局总局新疆维吾尔自治区税务局\n移动办公平台V1.0";
    self.textLabel.numberOfLines = 2;
    self.textLabel.font = FONT_SIZE(12);
    self.textLabel.textColor =  KGmColorWithHexColor(0xFF222222);
    self.textLabel.textAlignment = NSTextAlignmentCenter;
    self.textLabel.centerX = SCREEN_WIDTH/2;
    
    self.desclLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, SCREEN_HEIGHT - TabbarSafeBottomMargin - StatusBarAndNavigationBarHeight - kHConversions(30), SCREEN_WIDTH - 20, 12)];
    //    self.desclLabel.text = @"国家税务总局新疆维吾尔自治区税务局";
    self.desclLabel.font = FONT_SIZE(12);
    self.desclLabel.textAlignment = NSTextAlignmentCenter;
    self.desclLabel.textColor = KFontLightColor;
    
    [self.view addSubview:backView];
    [backView addSubview:iconImageView];
    [backView addSubview:self.textLabel];
    [self.view addSubview:self.desclLabel];
    
    //有新版本
    NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
    NSString *app_version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
    if ([self.version integerValue]>[[app_version stringByReplacingOccurrencesOfString:@"." withString:@""] integerValue]) {
        MOHintView *hintView = [[MOHintView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) andContentStr:@"发现新版本,是否更新？"];
        hintView.clickSureButtonBlock = ^{
            if (@available(iOS 10.0, *)) {
                [[UIApplication sharedApplication] openURL: [NSURL URLWithString:@"https://www.pgyer.com/geKO"] options: @{} completionHandler: nil];
            } else {
                [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"https://www.pgyer.com/geKO"]];
            }
        };
        [WINDOW addSubview:hintView];
    }
}


-(void)initData
{
    NSString *postUrl = [NSString stringWithFormat:@"%@?%@&authorization=%@",GETBYTYPE_URL,@"dictType=5",[NSString URLEncodedString:[MOUser sharedMOUser].token]];
    
    [PPNetworkHelper GET:postUrl parameters:@{} success:^(id responseObject) {
        NSLog(@"关于:%@",responseObject);
        
        NSArray *arr = responseObject;
        
        NSString *version;
        NSString *name;
        NSString *desc;
        for (int i = 0; i<arr.count; i++) {
            NSDictionary *dataDic = arr[i];

            NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
            NSString *app_version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
            version = app_version;

            if ([[NSString passNullForString:dataDic[@"dict_id"]] isEqualToString:@"5_2"]) {
                desc = [NSString passNullForString:dataDic[@"dict_value"]];
            }
            if ([[NSString passNullForString:dataDic[@"dict_id"]] isEqualToString:@"5_1"]) {
                name = [NSString passNullForString:dataDic[@"dict_value"]];
            }
        }
        self.textLabel.text = [NSString stringWithFormat:@"%@V%@",name,version];
        [self.textLabel changeLineSpaceForLabel:self.textLabel WithSpace:8];
        self.textLabel.textAlignment = NSTextAlignmentCenter;
        self.textLabel.centerX = SCREEN_WIDTH/2;
        
        self.desclLabel.text = desc;
        
    } failure:^(NSError *error) {
        [[ADShowMessage shareShowMessage] showTextOnly:[error localizedDescription] showInView:self.view];
    }];
}


@end
